package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.dao.AdditionalUserCommodityDao;
import com.qdlc.p2p.dal.dto.AdditionalUserCommodity;
import com.qdlc.p2p.dal.model.AdditionalUserCommodityModel;

/**
 * Created by liaoxiangkai on 2015/12/9.
 */
@Repository("additionalUserCommodityDao")
public class AdditionalUserCommodityDaoImpl extends BaseDaoImpl<AdditionalUserCommodity> implements AdditionalUserCommodityDao {

    /**
     * 用户获得商品列表
     *
     * @param model
     * @return
     */
    @Override
    public PageDataList<AdditionalUserCommodityModel> list(AdditionalUserCommodityModel model) {
        QueryParam param = QueryParam.getInstance().addPage(model.getPage(), model.getRows());
        if (StringUtil.isNotBlank(model.getSearchName())) {
            SearchFilter orFilter1 = new SearchFilter("user.userName", SearchFilter.Operators.LIKE, model.getSearchName().trim());
            SearchFilter orFilter2 = new SearchFilter("user.realName", SearchFilter.Operators.LIKE, model.getSearchName().trim());
            param.addOrFilter(orFilter1, orFilter2);
        } else {

            //用户id查询,前台查询使用
            if (null != model.getUser() && model.getUser().getUserId() > 0)
                param.addParam("user.userId", model.getUser().getUserId());
            if (model.getStatus() != 99)
                param.addParam("status", model.getStatus());

            //用户名查询
            if (StringUtil.isNotBlank(model.getUserName())) {
                SearchFilter orFilter1 = new SearchFilter("user.userName", SearchFilter.Operators.LIKE, model.getUserName().trim());
                SearchFilter orFilter2 = new SearchFilter("user.realName", SearchFilter.Operators.LIKE, model.getUserName().trim());
                param.addOrFilter(orFilter1, orFilter2);
            }
            //商品名称查询
            if (StringUtil.isNotBlank(model.getCommodityName()))
                param.addParam("additionalCommodity.name", SearchFilter.Operators.LIKE, model.getCommodityName().trim());
            //快递单号查询
            if (StringUtil.isNotBlank(model.getExpressNum()))
                param.addParam("expressNum", SearchFilter.Operators.LIKE, model.getExpressNum());
        }
        PageDataList<AdditionalUserCommodity> pageDataList = this.findPageList(param);
        PageDataList<AdditionalUserCommodityModel> modelList = new PageDataList<AdditionalUserCommodityModel>();
        List<AdditionalUserCommodityModel> list = new ArrayList<AdditionalUserCommodityModel>();
        if (null != pageDataList.getList() && pageDataList.getList().size() > 0) {
            for (AdditionalUserCommodity entity : pageDataList.getList()) {
                AdditionalUserCommodityModel additionalUserCommodityModel = AdditionalUserCommodityModel.instance(entity);
                //设置审核人名字
                if (null != entity.getAuditOperator())
                    additionalUserCommodityModel.setAuditName(entity.getAuditOperator().getUserName());
                //设置发货人名字
                if (null != entity.getPostOperator())
                    additionalUserCommodityModel.setPostName(entity.getPostOperator().getUserName());
                //设置用户名，获得物品名称
                if (null == entity.getUser() || null == entity.getAdditionalCommodity()) continue;
                additionalUserCommodityModel.setUserName(entity.getUser().getUserName());
                additionalUserCommodityModel.setRealName(entity.getUser().getRealName());
                additionalUserCommodityModel.setCommodityName(entity.getAdditionalCommodity().getName());
                list.add(additionalUserCommodityModel);
            }
        }
        modelList.setPage(pageDataList.getPage());
        modelList.setList(list);
        return modelList;
    }

    /**
     * 单个ip总记录数
     * @param addIp
     * @return
     */
    @Override
    public int SingleIpCount(String addIp) {
        String jqpl = "select count(*) from AdditionalUserCommodity where addIp =:addIp";
        Query query = em.createQuery(jqpl);
        query.setParameter("addIp", addIp);
        return ((Long)query.getSingleResult()).intValue();
    }
    /**
     * 审核用户获得物品
     *
     * @param model
     * @return
     */
    @Override
    public int doAudit(AdditionalUserCommodityModel model) {
        String jpql = "update AdditionalUserCommodity set status =:status, auditOperator =:auditOperator, auditTime =:auditTime ";
        //如果审核有备注
        if (StringUtil.isNotBlank(model.getAuditRemark()))
            jpql += ", auditRemark =:auditRemark ";
        jpql += "where id=:id";
        Query query = em.createQuery(jpql);
        if (StringUtil.isNotBlank(model.getAuditRemark()))
            query.setParameter("auditRemark", model.getAuditRemark());
        query.setParameter("status", model.getStatus());
        query.setParameter("auditOperator", model.getAuditOperator());
        query.setParameter("auditTime", DateUtil.getNow());
        query.setParameter("id", model.getId());
        return query.executeUpdate();
    }

    /**
     * 发货操作
     *
     * @param model
     */
    @Override
    public int doPost(AdditionalUserCommodityModel model) {
        String jpql = "update AdditionalUserCommodity set status=:status, expressName=:expressName, expressNum=:expressNum, postTime=:postTime, postOperator=:postOperator where id=:id";
        Query query = em.createQuery(jpql);
        query.setParameter("status", AdditionalUserCommodityModel.USER_COMMODITY_SENT);
        query.setParameter("expressName", model.getExpressName());
        query.setParameter("expressNum", model.getExpressNum());
        query.setParameter("postTime", DateUtil.getNow());
        query.setParameter("postOperator", model.getPostOperator());
        query.setParameter("id", model.getId());
        return query.executeUpdate();
    }

}
